package cn.ligoo.part.web.springmvc.platform;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import cn.ligoo.part.domain.SysModAction;
import cn.ligoo.part.service.impl.FacadeImpl;
import cn.ligoo.part.web.Constants;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/platform/sys-mod-action")
public class SysModActionController {

	private final Logger logger = LoggerFactory.getLogger(SysModActionController.class);

	@Resource
	private MessageSource message;

	@Resource
	private FacadeImpl facade;

	public SysModActionController() {
		logger.debug("...constructor");
	}

	@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String save(HttpServletRequest request, @RequestParam String role_code) {
		logger.debug("...save");
		logger.debug("role_code={}", role_code);

		try {
			SysModAction t = new SysModAction();
			t.setRole_code(role_code);
			// 先删
			facade.getSysModActionService().removeSysModAction(t);

			// 再增
			// m_(ROLE_CODE)_(ACTION_CODE)
			Map<String, Object> map = WebUtils.getParametersStartingWith(request, "m_");
			String[] names;
			for (String key : map.keySet()) {
				logger.debug("{}:=={}", key, map.get(key));
				if (null == map.get(key)) {
					continue;
				}
				names = key.split("_");
				t.setId(null);
				t.setMod_code(Integer.parseInt(names[0]));
				t.setAction_code(names[1]);
				facade.getSysModActionService().createSysModAction(t);
			}
			// @SuppressWarnings("rawtypes")
			// Enumeration e = request.getParameterNames();
			// String[] names;
			// while (e.hasMoreElements()) {
			// String pname = (String) e.nextElement();
			// String value = request.getParameter(pname);
			// logger.debug("{}:=={}", pname, value);
			// logger.debug("{}.startsWith(\"m_\"):==", pname, pname.startsWith("m_"));
			// if (null != value && pname.startsWith("m_")) {
			// names = pname.split("_");
			// t.setId(null);
			// t.setMod_code(Integer.parseInt(names[1]));
			// t.setAction_code(names[2]);
			// facade.getSysModActionService().createSysModAction(t);
			// }
			//
			// }
		} catch (DuplicateKeyException dke) {
			String msg = message.getMessage("errors.dae.DuplicateKeyException", null, null);
			return String.format(Constants.JSON_ERROR, msg);
		} catch (DataAccessException dae) {
			String msg = message.getMessage("errors.dae", null, null);
			return String.format(Constants.JSON_ERROR, msg);
		}

		return String.format(Constants.JSON_SUCCESS, null, null);
	}

	@RequestMapping(value = "/json", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String json(@RequestParam String role_code) {
		logger.debug("...json");

		SysModAction t = new SysModAction();
		t.setRole_code(role_code);
		logger.debug("...sysModAction:{}", t.toString());

		List<SysModAction> sysModActionList = facade.getSysModActionService().getSysModActionList(t);

		String json = JSON.toJSONString(sysModActionList);
		logger.debug("...json:{}", json);
		return json;
	}
}
